package com.telecomyt.jwportal.http.base;

import com.telecomyt.jwportal.utils.KLog;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author created by chengDa
 * @time 2020/8/31  16:48
 * @description
 */
public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {
    private static final String TAG = "http";

    @Override
    public void onSubscribe(Disposable d) {
        KLog.e(TAG, "onSubscribe: ");
    }

    @Override
    public void onNext(BaseResponse<T> response) {
        // 基础数据 进行统一处理
        if (response.getCode() == 200) {
            onSuccess(response.getData());
        } else {
            onFailure(null, response.getMessage());
        }
    }

    @Override
    public void onError(Throwable e) {
        KLog.e(TAG, "Throwable: " + e.getMessage());
    }

    @Override
    public void onComplete() {
        KLog.e(TAG, "onComplete: ");
    }

    public abstract void onSuccess(T data);

    public abstract void onFailure(Throwable e, String errorMsg);
}